
package net.atremmedia.gwt.actionjava.examples.graphics.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.anim.Anim;
import actionjava.anim.config.Manifest;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.geom.Color;
import com.google.gwt.user.client.Timer;

public class GraphicsExample1 extends AJExample
{
	public static final String NAME = "GraphicsExample 1";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-graphics/src/main/java/net/atremmedia/gwt/actionjava/examples/graphics/samples/GraphicsExample1.java";

	private Sprite testSprite;
	private Timer tweenTimer;

	public GraphicsExample1(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		testSprite = new Sprite();
		testSprite.setX(400);
		testSprite.setY(150);

		try {
			Shape shape1 = new Shape();
			shape1.setX(74);
			shape1.setY(16);
			Graphics graph = shape1.getGraphics();
			graph.beginFill(Color.getRGBA(0xCC9966, 1));
			graph.decodePath("AlvDGQiYhTAAhzQAAhzCYhRQCZhSDWAAQDXAACYBSQCZBRAABzQAABziZBTQiYBSjXAAQjWAAiZhSg");
			graph.endFill();
			testSprite.addChild(shape1);

			Shape shape2 = new Shape();
			shape2.setX(5.5);
			shape2.setY(111);
			graph = shape2.getGraphics();
			graph.beginFill(Color.getRGBA(0xFF0000, 1));
			graph.decodePath("Ai3F8IAAr3IFwAAIAAL3g");
			graph.endFill();
			testSprite.addChild(shape2);

			Shape shape3 = new Shape();
			shape3.setX(213);
			shape3.setY(53.5);
			graph = shape3.getGraphics();
			graph.beginFill(Color.getRGBA(0x006699, 1));
			graph.decodePath("AiyC5IigAAIAAgKIAAihIAAjFIKlAAIAADFIAACrg");
			graph.endFill();
			testSprite.addChild(shape3);

			Shape shape4 = new Shape();
			shape4.setX(208.5);
			shape4.setY(119.5);
			graph = shape4.getGraphics();
			graph.beginFill(Color.getRGBA(0x00FF99, 1));
			graph.decodePath("AKjHlIAAgKIEEAAIAAAKgAkLBBIqaAAIAAokIIGAAIB6AAIAAAJICgAAIAAIbg");
			graph.endFill();
			testSprite.addChild(shape4);

			Shape shape5 = new Shape();
			shape5.setX(76.4);
			shape5.setY(116.5);
			graph = shape5.getGraphics();
			graph.beginFill(Color.getRGBA(0x0066CC, 1));
			graph.decodePath("AoSUTIg1AZIhdhjItwm6IBAmkIlDlVINb4lIbgFJIAIA7IPsAAIAAImIhuAAIAAjIIqoAAIAADIIiOAAIAVCgIoHAAIAAIkIKaAAIB8DwIiVCXIAJBFIiSBFIuVOggAt8FFIFyAAIAAr2IlyAAgAmGyxQiYBSAAB0QAAB0CYBSQCZBSDXAAQDVAACZhSQCYhSAAh0QAAh0iYhSQiZhSjVAAQjXAAiZBSg");
			graph.endFill();
			testSprite.addChild(shape5);
		} catch (Exception e) {
			e.printStackTrace();
		}

		addChild(testSprite);

		animate();
		initTimer();
	}

	private void initTimer()
	{
		tweenTimer = new Timer()
		{
			@Override
			public void run()
			{
				animate();
			}
		};
		tweenTimer.scheduleRepeating(3100);
	}

	private void animate()
	{
		Anim.tween(new Manifest(testSprite, 3, getRandomEase())
		{
			@Override
			protected void configure()
			{
				fromTo(X, 0, getRandomX());
				fromTo(Y, 0, getRandomY());
			}
		});
	}

	private double getRandomX()
	{
		double x = getRandomNumber(100, 700);
		if(Math.abs(testSprite.getX() - x) < 200) {
			x += (x < testSprite.getX()) ? -200 : 200;
			x = (x < 100) ? 100 : x;
			x = (x > 700) ? 700 : x;
		}
		return x;
	}

	private double getRandomY()
	{
		double y = getRandomNumber(100, 700);
		if(Math.abs(testSprite.getY() - y) < 200) {
			y += (y < testSprite.getY()) ? -200 : 200;
			y = (y < 100) ? 100 : y;
			y = (y > 700) ? 700 : y;
		}
		return y;
	}

	@Override
	public void destroy()
	{
		super.destroy();
		tweenTimer.cancel();
	}
}
